package com.parking.carsystem.parkingchargesystem.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.parking.carsystem.parkingchargesystem.R;
import com.parking.carsystem.parkingchargesystem.activity.PaySettingActivity;
import com.parking.carsystem.parkingchargesystem.common.Constant;
import com.parking.carsystem.parkingchargesystem.module.GroupListModule;
import com.parking.carsystem.parkingchargesystem.utils.SPUtils;
import com.parking.carsystem.parkingchargesystem.view.button.SwitchView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class PaySettingAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public ArrayList<GroupListModule.DataBean.VehiclesBean> vehiclesBeans;

    public PaySettingActivity activity;

    public PaySettingAdapter(ArrayList<GroupListModule.DataBean.VehiclesBean> dataBeans, PaySettingActivity activity) {
        this.vehiclesBeans = dataBeans;
        this.activity = activity;
    }

    @Override
    public int getItemViewType(int position) {
        GroupListModule.DataBean.VehiclesBean vehiclesBean = vehiclesBeans.get(position);
        if (vehiclesBean.type) {
            //标题
            return Constant.VIEWTYPE_THREE;
        } else {
            //内容
            return Constant.VIEWTYPE_OTHER;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == Constant.VIEWTYPE_THREE) {
            return new AddViewHodler(LayoutInflater.from(activity).inflate(R.layout.item_pay_setting_type, parent, false));
        } else {
            return new ParkingViewHodler(LayoutInflater.from(activity).inflate(R.layout.item_paking_pay_setting, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final GroupListModule.DataBean.VehiclesBean vehiclesBean = vehiclesBeans.get(position);
        if (vehiclesBean.type) {
            //标题
            AddViewHodler addViewHodler = (AddViewHodler) holder;
            addViewHodler.company_title.setText(vehiclesBean.plateNumber);
        } else {
            final ParkingViewHodler parkingViewHodler = (ParkingViewHodler) holder;
            //车牌内容
            //正常的车牌
            parkingViewHodler.parking_name.setText(vehiclesBean.plateNumber);
            parkingViewHodler.open_or_close.setOpened(vehiclesBean.unconsciousPay);
            if (vehiclesBean.plateColor == 1) {
                parkingViewHodler.layout_blue.setText("黄车牌");
                parkingViewHodler.layout_blue.setBackgroundResource(R.drawable.car_parking_yellow_text);
                parkingViewHodler.layout_blue.setTextColor(activity.getResources().getColor(R.color.white));
            } else if (vehiclesBean.plateColor == 0) {
                parkingViewHodler.layout_blue.setText("蓝车牌");
                parkingViewHodler.layout_blue.setBackgroundResource(R.drawable.car_parking_blue_text);
                parkingViewHodler.layout_blue.setTextColor(activity.getResources().getColor(R.color.white));
            } else if (vehiclesBean.plateColor == 2) {
                parkingViewHodler.layout_blue.setText("特种牌");
                parkingViewHodler.layout_blue.setBackgroundResource(R.drawable.car_parking_gray_text);
                parkingViewHodler.layout_blue.setTextColor(activity.getResources().getColor(R.color.white));
            }
            parkingViewHodler.open_or_close.setOnButtonItemClickListener(new SwitchView.OnButtonItemClickListener() {
                @Override
                public void onClick() {
                    String userid = SPUtils.getInstance(activity).getString(Constant.USER_ID);
                    if(userid.equals(vehiclesBean.vehicleOwnerId) && !TextUtils.isEmpty(vehiclesBean.vehicleOwnerId)){
                        //调用接口
                        vehiclesBean.unconsciousPay = !vehiclesBean.unconsciousPay;
                        parkingViewHodler.open_or_close.setOpened(vehiclesBean.unconsciousPay);
                        unconsciousPay(vehiclesBean.plateNumber,vehiclesBean.unconsciousPay);
                    }else {
                        activity.showToast("暂无权限");
                    }
                }
            });
        }
    }

    /**
     * 调用设置权限用户
     * @param plateNumber
     * @param opera
     */
    private void unconsciousPay(String plateNumber, boolean opera) {
        HashMap<String,String> hashMap = new HashMap<>();
        hashMap.put("plateNumber",plateNumber);
        hashMap.put("opera",String.valueOf(opera));
        JSONObject jsonObject =  new JSONObject(hashMap);
        OkGo.<String>post(Constant.url+Constant.UNCONSCIOUSPAY)
                .upJson(jsonObject.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                    }

                    @Override
                    public void onError(Response<String> response) {

                    }
                });
    }

    @Override
    public int getItemCount() {
        return vehiclesBeans == null ? 0 : vehiclesBeans.size();
    }

    public void setDatas(ArrayList<GroupListModule.DataBean.VehiclesBean> dataBeans) {
        this.vehiclesBeans = dataBeans;
        this.notifyDataSetChanged();
    }

    static class AddViewHodler extends RecyclerView.ViewHolder {
        TextView company_title;

        public AddViewHodler(View itemView) {
            super(itemView);
            company_title = itemView.findViewById(R.id.company_title);
        }
    }

    static class ParkingViewHodler extends RecyclerView.ViewHolder {
        TextView layout_blue;
        TextView parking_name;
        SwitchView open_or_close;

        public ParkingViewHodler(View itemView) {
            super(itemView);
            layout_blue = itemView.findViewById(R.id.layout_blue);
            parking_name = itemView.findViewById(R.id.parking_name);
            open_or_close = itemView.findViewById(R.id.open_or_close);
        }
    }
}
